﻿using System.Configuration;
using System.Web.Mvc;
using System.Web.Routing;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using MvcContrib.Unity;

namespace Web
{
    public class GlobalApplication : System.Web.HttpApplication, IUnityContainerAccessor
    {
        private static UnityContainer unityContainer;

        static GlobalApplication()
        {
            unityContainer = new UnityContainer();
        }

        public static IUnityContainer Container
        {
            get
            {
                return unityContainer;
            }
        }

        IUnityContainer IUnityContainerAccessor.Container
        {
            get
            {
                return Container;
            }
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            RegisterRoutes(RouteTable.Routes);

            IControllerFactory controllerFactory = new UnityControllerFactory();
            ControllerBuilder.Current.SetControllerFactory(controllerFactory);

            UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
            section.Containers.Default.Configure(unityContainer);
        }
    }
}